package com.xh.nci;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;

public class UserList {

    /**
     * @param args
     */
    public static void main(String[] args) {

        String host = "117.79.147.162"; // AD服务器
        String port = "389"; // 端口
        String url = new String("ldap://" + host + ":" + port);

        Hashtable HashEnv = new Hashtable();
        String adminName = "khdev\\administrator"; // 注意用户名的写法：domain\User
        String adminPassword = "kh@2021"; // 密码
        HashEnv.put(Context.SECURITY_AUTHENTICATION, "simple"); // LDAP访问安全级别
        HashEnv.put(Context.SECURITY_PRINCIPAL, adminName); // AD User
        HashEnv.put(Context.SECURITY_CREDENTIALS, adminPassword); // AD Password
        HashEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); // LDAP工厂类
        HashEnv.put(Context.PROVIDER_URL, url);


        // 基于baseDN查询
        String baseDN = "dc=khdev,dc=com";
        String objectCategory = "objectCategory=cn=Person,cn=Schema,cn=Configuration," + baseDN;
        String filter = "(objectClass=top)";
        filter = "(&(objectClass=person)" + filter + ")";
        filter = "(&(objectClass=user)" + filter + ")";
        filter = "(&(objectClass=organizationalPerson)" + filter + ")";
        filter = "(&(" + objectCategory + ")" + filter + ")";
        try {
            LdapContext ctx = new InitialLdapContext(HashEnv, null);
            // 实例化一个搜索器
            SearchControls cons = new SearchControls();
            // 搜索范围： 1、平级检索；2、树形检索
            cons.setSearchScope(SearchControls.SUBTREE_SCOPE);
            //设置为false时返回结果占用内存减少
            cons.setReturningObjFlag(true);
            // 执行查询
            NamingEnumeration<SearchResult> sEnum = ctx.search(baseDN, filter, cons);
            int size = 0;
            while (sEnum.hasMoreElements()) {
                SearchResult sr = sEnum.nextElement();
                String DN = sr.getName();
                System.out.println("DN: " + DN);
                Attributes attrs = sr.getAttributes();
                // 取到所有属性
                NamingEnumeration<? extends Attribute> aEnum = attrs.getAll();
                while (aEnum.hasMoreElements()) {
                    Attribute attr = aEnum.nextElement();
                    if (attr == null) {
                        continue;
                    }
                    // 打印属性名和属性值，属性值可以为多个
                    System.out.print("id = " + attr.getID() + ", value = ");
                    for (int i = 0; i < attr.size(); i++) {
                        Object obj = attr.get(i);
                        System.out.print(obj + " ");
                    }
                    System.out.println();
                }
                size++;
                System.out.println("---------------------------------------");
            }
            System.out.println("共返回： " + size);
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }

}